import 'package:bt_dsp_88/model/model_send.dart';
import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../model/model_default.dart';
import 'search_row.dart';

class SearchView extends BLESearchDeviceDialog implements BLESearchDeviceDialogDelegate {
  SearchView({super.key, super.height = 520}) : super() {
    delegate = this;
  }
  @override
  Color? get contentColor => Colors.white;

  @override
  Widget? bgViewForFooter(Widget child) => null;

  @override
  Widget? bgViewForHeader(Widget child) => null;

  @override
  List<String>? filterName() => ['DSP8.8BT'];
  @override
  List<RowModel> listModelForFooter(String appVersion) {
    return ['Refresh Device List', 'Reset DSP Tuning', 'Version: $appVersion']
        .map(($0) => RowModel(
            text: $0,
            textStyle: const TextStyle(fontSize: 14, color: Colors.black),
            height: isGroupManager ? 60 : 40,
            bgColor: Colors.transparent,
            lineColor: Colors.black,
            lineHeight: 1,
            iconName: $0,
            iconSize: 25))
        .toList();
  }

  @override
  List<RowModel> listModelForHeader(String appVersion) {
    return ['Devices']
        .map(($0) => RowModel(
            text: $0,
            height: 60,
            textStyle: const TextStyle(fontSize: 20, color: Colors.black),
            lineColor: Colors.black,
            lineHeight: 1,
            bgColor: Colors.transparent))
        .toList();
  }

  @override
  List<RowModel> listModelForSection(List<String> list) {
    return list
        .map(($0) => RowModel(
            text: $0,
            height: 40,
            textStyle: const TextStyle(fontSize: 14, color: Colors.black),
            lineColor: Colors.black,
            lineHeight: 1,
            bgColor: Colors.transparent,
            iconName: '',
            iconSize: 20))
        .toList();
  }

  @override
  void methodForClickBLESearchTableView(BLESearchTableViewClickType type, String tag) {
    if (type == BLESearchTableViewClickType.footer) {
      switch (tag) {
        case 'Refresh Device List':
          // refreshForPullDownMethod();
          refreshKey?.currentState?.show();
        case 'Reset DSP Tuning':
          if (BluetoothManager.instance.connectedDeviceList.isEmpty) return;
          showAlert(state.context, 'Are you sure to reset DSP tuning?', ['NO', 'YES'], (value) {
            if (value == 'YES') {
              globalModel.sendToDevice(tag: 'RESET_DSP');
            }
          });
        default:
          break;
      }
    }
  }

  @override
  void refreshMethodExentesion() {}

  @override
  Widget viewForRowInSection(DSPeripheral peripheral, int section, int row, bool isOn) {
    return SearchItem(
        device: peripheral.peripheral!,
        localName: peripheral.name,
        onSelect: () => clickDevice(section, row),
        onPressLongToChangeName: () => changeDeviceName(section, row, peripheral.peripheral, null),
        ison: isOn);
  }
}
